<?php
/**
 * Classe Utilitaria para gerenciamento e envio de emails
 * @author Wiliame Morais @pc4Team
 *
 */
class PC4_Util_DispachanteEmail{

	/**
	 * Funcao que envia email
	 * @param string $email
	 * @param string $nome
	 * @param string $assunto
	 * @param string $conteudo
	 * @param string $controlador
	 * @param string $fromEmail
	 * @param string $fromName
	 * @param Zend_Mail_Transport $transport
	 */
	static function envia($email, $nome, $assunto, $conteudo, $controlador, $fromEmail = null, $fromName = null, $transport = null){
	
		$bootstrap = $controlador->getInvokeArg('bootstrap');
 	   	$siteConfig = $bootstrap->getOption('site');
    	$resources = $bootstrap->getOption('resources');
		$transportConfig = $resources['mail']['transport'];    	
		
		if(!isset($transport)){
			$transport = new Zend_Mail_Transport_Smtp(
				$transportConfig['host'], 
				array(
					'port' => $transportConfig['port'], 
					'auth' => $transportConfig['auth'], 
					'username' => $transportConfig['username'],
					'password' => $transportConfig['password']
				)
			);
		}
		
		$fromEmail = (empty($fromEmail)) ? $siteConfig['emailadministrador'] : $fromEmail;
		$fromName = (empty($fromName)) ? $siteConfig['titulo'] : $fromName;
				
		$mail = new Zend_Mail();
        $mail->setBodyHtml(utf8_decode($conteudo));
		$mail->setFrom($fromEmail, $fromName);
		$mail->addTo($email, utf8_decode($nome));
		$mail->setSubject(utf8_decode($assunto));
		try {
			$ok = $mail->send($transport);
		}
		catch(Zend_Mail_Exception $e) {
			$ok = $e->getMessage();
		}
		return $ok;
	}
}